package com.example.feifei.newproject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.feifei.utils.Player;
import com.example.feifei.view.banner.Banner;
import com.example.feifei.view.banner.GlideImageLoader;
import com.example.feifei.view.banner.OnBannerListener;
import com.othershe.calendarview.CalendarView;
import com.othershe.calendarview.DateBean;
import com.othershe.calendarview.WeekView;
import com.othershe.calendarview.listener.OnMonthItemClickListener;
import com.othershe.calendarview.listener.OnPagerChangeListener;
import com.othershe.calendarview.utils.SolarUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by feifei on 2017/10/13.
 */

public class PagerActivity extends Activity implements OnBannerListener,View.OnClickListener{
    //轮播组件
    Banner banner;
    //轮播显示图片地址
    List<String> imageUrls;
    //音乐进度条
    private SeekBar seekbar_player;
    //音乐播放控制类
    private Player mediaPlayer;
    //音乐播放控制
    private Button btnPlayUrl,btnPause,btnStoop,btnReplay;
    //月日历播放
    private Button btnMonthDate;
    //测试日期显示
    private TextView test_date;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager);
        //图片轮播
        initPagerScroll();
        //音乐播放
        initMediaPlay();
        //月日历弹窗
        initMonthDate();
    }

    @Override
    protected void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        banner.stopAutoPlay();
        mediaPlayer.stop();
    }

    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(this,"点击了图片=="+position,Toast.LENGTH_SHORT).show();
    }
    //图片轮播开始播放
    private void initPagerScroll(){
        banner = (Banner) this.findViewById(R.id.banner);
        imageUrls = new ArrayList<>();
        imageUrls.add("http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png");
        imageUrls.add("http://img.zcool.cn/community/01fca557a7f5f90000012e7e9feea8.jpg");
        imageUrls.add("http://img.zcool.cn/community/01996b57a7f6020000018c1bedef97.jpg");
        imageUrls.add("http://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg");
        //简单使用
        banner.setImages(imageUrls)
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(this)
                .start();
    }
    //进行音乐播放
    private void initMediaPlay(){
        //初始化组件
        seekbar_player = (SeekBar) this.findViewById(R.id.seekbar_player);
        seekbar_player.setOnSeekBarChangeListener(new SeekBarChangeEvent());

        btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
        btnPlayUrl.setOnClickListener(this);

        btnPause = (Button) this.findViewById(R.id.btnPause);
        btnPause.setOnClickListener(this);

        btnStoop = (Button) this.findViewById(R.id.btnStoop);
        btnStoop.setOnClickListener(this);

        btnReplay = (Button) this.findViewById(R.id.btnReplay);
        btnReplay.setOnClickListener(this);

        //进行音乐播放设置
        String videoUrl = "http://59.110.173.105/upload/video.mp3";
        mediaPlayer = new Player(this,videoUrl,seekbar_player);
        //进行电话监听设置
        TelephonyManager telephonyManager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }

    //月日历初始化
    private void initMonthDate(){
        btnMonthDate = (Button) this.findViewById(R.id.btnMonthDate);
        btnMonthDate.setOnClickListener(this);
        test_date = (TextView) this.findViewById(R.id.test_date);
//        test_date.setText(SolarUtil.getMonthAgo());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //播放
            case R.id.btnPlayUrl:
                mediaPlayer.play();
                btnPlayUrl.setText("开始播放...");
                break;
            //暂停
            case R.id.btnPause:
                boolean pause=mediaPlayer.pause();
                if (pause) {
                    btnPause.setText("继续");
                    btnPause.setText("暂停播放...");
                }else{
                    btnPause.setText("暂停");
                    btnPause.setText("继续播放...");
                }
                break;
            //停止
            case R.id.btnStoop:
                mediaPlayer.stop();
                btnStoop.setText("停止播放...");
                break;
            //重播
            case R.id.btnReplay:
                mediaPlayer.replay();
                btnReplay.setText("重新播放...");
                break;
            //月日历实现
            case R.id.btnMonthDate:
                showMonthDialog();
                break;

        }
    }

    /**
     * 展示月日历
     */
    private void showMonthDialog() {
        final View viewDate = LayoutInflater.from(this).inflate(R.layout.dialog_month,null);
        final Dialog dateDialog = new AlertDialog.Builder(this).create();
        final TextView dateTitle = (TextView) viewDate.findViewById(R.id.dateTitle);
        //上个月按钮
        ImageView btn_lastMonth = (ImageView) viewDate.findViewById(R.id.btn_lastMonth);
        //下个月按钮
        ImageView btn_nextMonth = (ImageView) viewDate.findViewById(R.id.btn_nextMonth);

        final CalendarView calendarView = (CalendarView) viewDate.findViewById(R.id.calendar);
        calendarView.init();

        dateDialog.show();
        dateDialog.setContentView(viewDate);

        DateBean d = calendarView.getDateInit();
        //为标题设置日期
        dateTitle.setText(d.getSolar()[0] + "年" + d.getSolar()[1] + "月" + d.getSolar()[2] + "日");
        //监听页面滑动
        calendarView.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                dateTitle.setText(date[0] + "年" + date[1] + "月" + date[2] + "日");
            }
        });
        //监听点击事件
        calendarView.setOnItemClickListener(new OnMonthItemClickListener() {
            @Override
            public void onMonthItemClick(View view, DateBean date) {
                dateTitle.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月" + date.getSolar()[2] + "日");
                test_date.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月" + date.getSolar()[2] + "日");
                dateDialog.dismiss();
            }
        });
        //上个月点击事件
        btn_lastMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendarView.lastMonth();
            }
        });
        //下个月点击事件
        btn_nextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendarView.nextMonth();
            }
        });

    }



    /**
     * 只有电话来了之后才暂停音乐的播放
     */
    private final class MyPhoneListener extends android.telephony.PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING://电话来了
                    mediaPlayer.callIsComing();
                    break;
                case TelephonyManager.CALL_STATE_IDLE: //通话结束
                    mediaPlayer.callIsDown();
                    break;
            }
        }
    }

    /**
     * 监听进度条的变化
     */
    class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
        int progress;
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
            this.progress = progress * mediaPlayer.mediaPlayer.getDuration()
                    / seekBar.getMax();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // seekTo()的参数是相对与影片时间的数字，而不是与seekBar.getMax()相对的数字
            mediaPlayer.mediaPlayer.seekTo(progress);
        }
    }
}
